//
// Created by darcy on 4/23/21.
//

#include "../Include/Park.h"

using namespace std;

Park::Park(int n) {
    income = 0;
    N = n;
    spaces = new Automobile *[N];
    for (int x = 0; x < N; ++x)
        spaces[x] = nullptr;
    i = 0;
}

void Park::showInfo() const {
    cout << "停车场目前停放了" << i << "辆汽车：";
    for (int x = 0; x < N; ++x)
        if (spaces[x] != nullptr)
            cout << spaces[x]->showLicensePlate() << "，";
    cout << "共收入" << income << "元停车费" << endl;
}

void Park::enter(Automobile *automobile) {
    if (i == N)
        cout << "无法为" << automobile->showLicensePlate() << "分配停车位"
             << endl;
    else {
        ++i;
        int x;
        for (x = 0; x < N; ++x)
            if (spaces[x] == nullptr) {
                spaces[x] = automobile;
                cout << automobile->showLicensePlate()
                     << "进入停车场，分配停车位" << endl;
                break;
            }
    }
}

void Park::leave(Automobile *automobile) {
    --i;
    int x;
    bool t = true;
    for (x = 0; x < N; ++x)
        if (spaces[x] == automobile) {
            spaces[x] = nullptr;
            income += automobile->showFee();
            cout << automobile->showLicensePlate() << "离开停车场，缴纳停车费"
                 << automobile->showFee() << "元" << endl;
            break;
        }
    while (t) {
        t = false;
        for (x = 0; x < N - 1; ++x)
            if (spaces[x] == nullptr && spaces[x + 1] != nullptr) {
                swap(spaces[x], spaces[x + 1]);
                t = true;
            }
    }
}
